Ext.ns('App.module.Page');
App.module.Page.Search = Ext.extend(Ext.util.Observable, {
	constructor: function(config){
		App.module.Page.Add.superclass.constructor.call(this);	
	}
	,init: function(grid){
		grid.on('render', function(grid){
			var textField = new Ext.form.TextField({
				width: 200
				,emptyText: 'Search...'
				,listeners: {
					specialkey: function(field, e){
						if(e.getKey() != e.ENTER) return;
						doSearch();	
					}	
				}
			});
			
			var columns = grid.getColumnModel().config;
			
			var store = [];
			var value = columns[0].dataIndex;
			for(var i = 0; i < columns.length; i ++){
				if(false === columns[i].searchable) continue;
				store.push([columns[i].dataIndex, columns[i].header]);
				if(columns[i].defaultSearch) value = columns[i].dataIndex;
			}
			
			var combo = new Ext.form.ComboBox({
				store: store
				,width: 100
				,triggerAction: 'all'
				,forceSelection: true
				,value: value
			});
			
			grid.getTopToolbar().add('->', textField, combo);
			
			// do search
			function doSearch(){
				var params = {
					start: 0
					,limit: pageSize
					,field: combo.getValue()
					,value: textField.getValue()
				};
				
				grid.getStore().load({params: params});	
			}
		});
	}
});